#include "MainWindow.h"
#include "ui_MainWindow.h"


#include "CampaignSettingDataForm.h"


#include <CampaignSetting.h>
#include <CharacterClass.h>
#include <ReferenceItem.h>
#include <FileHandler.h>
#include <XmlSerializer.h>


#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    qRegisterMetaType<CampaignSetting>("CampaignSetting");
    qRegisterMetaType<CharacterClass>("CharacterClass");

    qRegisterMetaType<ValueItem>("ValueItem");
    qRegisterMetaType<RootItem>("RootItem");
    qRegisterMetaType<ReferenceItem>("RerferenceItem");

    installEventFilter(this);
    FileHandler::installSerializer(new XmlSerializer(this));

    ui->setupUi(this);

    on_tabWidget_currentChanged();
}


MainWindow::~MainWindow()
{
    delete ui;
}


bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
    if (o != this) return false;
    if (e->type() != QEvent::Close) return false;

    on_actionClose_All_triggered();

    return false;
}


void MainWindow::onDirty()
{
    for (int index = 0; index < ui->tabWidget->count(); index++)
    {
        QWidget* widget = ui->tabWidget->widget(index);
        if (!widget) continue;

        QString name = widget->property(P_CAMPAIGN_NAME).toString();
        if (name.isEmpty()) name = tr("Unnamed Campaign");
        ui->tabWidget->setTabText(index, name);
    }
}


void MainWindow::on_actionNew_triggered()
{
    CampaignSetting* root = dynamic_cast<CampaignSetting*>(FileHandler::open(":/d20/CAMPAIGN_SETTING"));
    if (!root)
    {
        QMessageBox::information(this, tr("Unknown Xml Content!"), tr("Resource file has unknown file content!"));
        return;
    }

    CampaignSettingDataForm* form = new CampaignSettingDataForm(root, this);
    ui->tabWidget->addTab(form, QString());
    ui->tabWidget->setCurrentWidget(form);

    onDirty();
    connect(form, SIGNAL(sigDirty()), this, SLOT(onDirty()));
}


void MainWindow::on_actionOpen_triggered()
{
    QString file_path = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath(), tr("Xml File (*.xml)"));
    if (file_path.isEmpty() || !QFile::exists(file_path)) return;

    CampaignSetting* root = dynamic_cast<CampaignSetting*>(FileHandler::open(file_path));
    if (!root)
    {
        QMessageBox::information(this, tr("Unknown Xml Content!"), tr("File has unknown file content!"));
        return;
    }

    CampaignSettingDataForm* form = new CampaignSettingDataForm(root, this);
    ui->tabWidget->addTab(form, QString());
    ui->tabWidget->setCurrentWidget(form);

    onDirty();
    connect(form, SIGNAL(sigDirty()), this, SLOT(onDirty()));
}


void MainWindow::on_actionSave_triggered()
{
    CampaignSettingDataForm* form = dynamic_cast<CampaignSettingDataForm*>(ui->tabWidget->currentWidget());
    if (!form) return;

    form->onSave();
}


void MainWindow::on_actionSave_As_triggered()
{
    CampaignSettingDataForm* form = dynamic_cast<CampaignSettingDataForm*>(ui->tabWidget->currentWidget());
    if (!form) return;

    form->onSaveAs();
}


void MainWindow::on_actionSave_All_triggered()
{
    for (int index = 0; index < ui->tabWidget->count(); index++)
    {
        CampaignSettingDataForm* form = dynamic_cast<CampaignSettingDataForm*>(ui->tabWidget->currentWidget());
        if (!form) continue;

        form->onSave();
    }
}


void MainWindow::on_actionClose_triggered()
{
    on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
}


void MainWindow::on_actionClose_All_triggered()
{
    int count = ui->tabWidget->count();
    while (count)
    {
        on_tabWidget_tabCloseRequested(0);

        if (count == ui->tabWidget->count()) return;
        count = ui->tabWidget->count();
    }
}


void MainWindow::on_actionQuit_triggered()
{
    close();
}


void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    QWidget* widget = ui->tabWidget->widget(index);
    CampaignSettingDataForm* form = dynamic_cast<CampaignSettingDataForm*>(widget);
    if (!form)
    {
        ui->tabWidget->removeTab(index);
        return;
    }

    form->onClose();
    ui->tabWidget->removeTab(index);

}






void MainWindow::on_tabWidget_currentChanged(int index)
{
    ui->menu_Edit->clear();
    CampaignSettingDataForm* form = dynamic_cast<CampaignSettingDataForm* >(ui->tabWidget->widget(index));
    if (!form)
    {
        ui->menu_Edit->setEnabled(false);
        return;
    }

    ui->menu_Edit->setEnabled(form->actions().count() != 0);
    ui->menu_Edit->addActions(form->actions());
}
